package web.video;

import entity.VideoEntity;
import util.FANREN;

import javax.persistence.EntityManager;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@WebServlet(name = "VideoImg",urlPatterns = "/dao/update/videoImg")
@MultipartConfig
public class VideoImg extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        EntityManager em = (EntityManager) request.getAttribute("_entity.Manager");
        String path = request.getServletContext().getRealPath("")+"img";//获取服务器当前目录
        System.out.println(path);
        Part part = request.getPart("file");
        String fileName = UUID.randomUUID() + ".jpg";
        if(!fileName.equals("")) {
            part.write(path + File.separator + fileName);
        }

        String vid = ""+request.getSession().getAttribute("myvid");
        VideoEntity videoEntity = em.find(VideoEntity.class,Integer.parseInt(vid));
        videoEntity.setImgUrl("http://localhost:8080/img/"+fileName);
        em.persist(videoEntity);
    }
}
